package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.junit.runners.Parameterized;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 菜单管理分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = menuService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result deleteById(Integer id) {
        try {
            menuService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

    /**
     * 查询新增窗口的父菜单编号为null的ID和名称
     * @return
     */
    @RequestMapping("/findNameAndId")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY','MENU_EDIT')")
    public List<Map<String,Object>> findNameAndId() {
        List<Map<String,Object>>map=menuService.findNameAndId();
       return map;
    }

    /**
     * 新增菜单项
     * @param menu
     * @return
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);

        }
    }

    /**
     * 回显编辑窗口数据
     * @param id
     * @return
     */
    @RequestMapping("/findMenuById")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY','MENU_EDIT')")
    public Result findMenuById(Integer id) {
        try {
            //查询此id的菜单项
            Menu menu=menuService.findMenuById(id);
            return new Result(true,MessageConstant.DATA_ECHO_SUCCESS,menu);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DATA_ECHO_FAIL);
        }
    }

    /**
     * 编辑菜单项
     * @param menu
     * @return
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);

        }
    }

    /**
     * 查询所有 菜单信息
     */
    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findAll(){
        try {
            List<Menu> menuList = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
}
